package com.sky.controller.admin;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

/**
 * @program: sky-take-out
 * @description:
 * @author: fmp
 * @create: 2023-04-09 15:10
 * @Version 1.0
 **/
@Slf4j
@RestController
@Api(tags = "通用工具类接口")
public class uploadController {
    @Resource
    private AliOssUtil aliOssUtil;
    @PostMapping("/admin/common/upload")
    public Result upload(MultipartFile file) throws IOException{
        //获取文件字节文件，和新名字
        String originalFilename = file.getOriginalFilename();
        String fileName =  IdUtil.simpleUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        byte[] fileBytes = new byte[0];
        try {
            fileBytes = file.getBytes();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("文件转换异常");
        }
        String uploadUrl = aliOssUtil.upload(fileBytes, fileName);
        return Result.success(uploadUrl);
    }
}
